package chapter06_object;

/**
 * ClassName: StudentTest
 * Package: OOP06
 * Description:
 *
 * @Author 安思宇
 * @Create 2025/3/4 16:41
 */
public class StudentTest {
    public static void main(String[] args) {
        //创建一个Student类型的数组
        Student[] students=new Student[20];
        for (int i = 0; i <20; i++) {
            students[i]=new Student();//创建了20个对象
            students[i].number=i+1;
            students[i].state=(int)(Math.random()*6+1);
            students[i].score=(int)(Math.random()*101);
        }
        //打印三年级的学生信息。
        boolean isEquals;
        System.out.println("三年级学生的信息如下：");
        for (int i = 0; i < students.length; i++) {
            if (students[i].state==3){
                System.out.println(students[i].sout());
            }
        }
        System.out.println("输出所有学生的信息：");
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i].sout());
        }
        //使用冒泡排序按学生成绩排序，并遍历所有的学生信息。
        System.out.println("按学生成绩输出所有学生的信息：");
        Student temp=new Student();
        for (int i = 0; i < students.length-1; i++) {
            boolean isFlag=true;
            for (int j = 0; j < students.length-1-i; j++) {
                if (students[j].score>students[j+1].score){
                    temp=students[j+1];
                    students[j+1]=students[j];
                    students[j]=temp;
                    isFlag=false;
                }
//                不满足实际需求，是错误的。
//                if (students[j].score>students[j+1].score){
//                    temp=students[j+1].score;
//                    students[j+1].score=students[j].score;
//                    students[j].score=temp;
//                    isFlag=false;
//                }

            }
            if(isFlag){
                break;
            }
        }

        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i].sout());
        }

    }
}
